// pages/masterSubOrder/masterSubOrder.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    index3: 0,
    index4: 0,
    index5: 0,
    orderInfo: null,
    canSub: true,
    baseImgPath: app.globalData.baseImgPath,
    backImgPath: app.globalData.URL,
    fbrand: [], //品牌
    fmaterial: ['光面、布面类', '麂皮、磨砂、珠光', '漆皮'], //材质
    fsize: ['大', '中', '小'], //大小
    date: '', //日期
    imgList: [],
    fprice: 0, //维修价格
    qrcode: '',
    brand:'',
    fcid:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options);
    var that = this;
    var qrcode=options.qrcode;
    that.setData({
      qrcode:qrcode
    })
    console.log('二维码：'+that.data.qrcode)
    wx.getStorage({
      key: 'sessionId',
      success: function(res) {
        that.setData({
          sessionId: res.data, //登陆页面取回来的sessionId
        });
        console.log(that.data.sessionId);
        //加载二维码返回数据
        wx.request({
          url: app.globalData.urlPath + '/ManagerMent/scanqrcode',
          data: {
            sessionid: app.globalData.sessionid,
            qrcode: that.data.qrcode
          },
          method: "POST",
          success: function (res) {
            console.log(res)
            if (res.data.code == 200) {
              let info = res.data.data;
              info.createtime = that.totime(info.createtime)
              that.setData({
                orderInfo: res.data.data,
                brand: res.data.data.fbrand,
                fcid: res.data.data.fcid
                // fbrand: res.data.data.fbrand,
                // fmaterial: res.data.data.fmaterial,
                // fsize: res.data.data.fsize,
              })
              for (var i = 0; i < that.data.fbrand.length; i++) {
                if (that.data.brand == that.data.fbrand[i].bname) {
                  that.setData({
                    index3: i
                  })
                }
              }
              // console.log('index3:' + that.data.index3)
              // console.log(that.data.fbrand[that.data.index3].bname)
              //查询品牌
              wx.request({
                url: app.globalData._urlPath + 'pindex/get_brand',
                data: {
                  cid: that.data.fcid
                },
                method: "get",
                success: function (res) {
                  console.log(res)
                  if (res.data.code == 200) {
                    that.setData({
                      fbrand: res.data.data,
                      fmaterial: ['光面、布面类', '麂皮、磨砂、珠光', '漆皮'],
                      fsize: ['大', '中', '小']
                    })

                    console.log(that.data.fbrand);




                  }
                  if (res.data.code == 400) {
                    wx.showToast({
                      title: res.data.error,
                      icon: 'none',
                      duration: 2000,
                      mask: true
                    })
                  }
                }
              });
            } else {
              wx.showToast({
                title: res.data.error,
                icon: 'none',
                duration: 2000
              })
              if (res.data.code == 5000) {
                wx.redirectTo({
                  url: '/pages/login/login',
                })
              }
            }
          }
        })
  

      },
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }, //上传图片
  //上传图片
  choose: function () { //这里是选取图片的方法
    var that = this;
    var pics = [];
    wx.chooseImage({
      count: 9 - pics.length, // 最多可以选择的图片张数，默认9
      sizeType: ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
      sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
      success: function (res) {
        console.log(res);
        var imgsrc = res.tempFilePaths;
        pics = that.data.imgList.concat(imgsrc);
        that.setData({
          pics: pics
        });

        that.uploadimg(imgsrc);
        //if (app.globalData.successN == imgsrc.length){
        // that.setData({
        //   imgList: app.globalData.pic
        // })
        that.setData({
          imgList: pics
        })
        console.log(that.data.pics)
        //}
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })

  },
  uploadimg: function (imgsrc) { //这里触发图片上传的方法
    var that = this;
    var pics = this.data.pics;
    console.log(pics)
    app.uploadimg1({
      url: app.globalData.urlPath + 'ManagerMent/uploadpic', //这里是你图片上传的接口
      path: imgsrc //这里是选取的图片的地址数组
    });
  },
  //删除上传的图片
  deletePic: function (e) {
    console.log(e)
    var self = this;
    wx.showToast({
      title: '删除成功！',
    })
    var imgs = self.data.imgList;
    var pics = app.globalData.pic;
    //console.log(pics)
    console.log(app.globalData.pic)
    var index = e.currentTarget.dataset.index;
    console.log(index)
    pics.splice(index, 1);
    imgs.splice(index, 1);

    self.setData({
      imgList: imgs
    });
    console.log(self.data.imgList)
    console.log(app.globalData.pic)

    wx.setStorage({
      key: 'imgPic',
      data: self.data.imgList,
    })
    // app.globalData.pic = self.data.imgList;
  },
  bindchange3: function(e) {
    var that = this;
    var index = e.detail.value
    console.log('picker发送选择改变，携带值为', (parseInt(index) + 1))
    this.setData({
      index3: index
    })
    console.log(that.data.fbrand[that.data.index3].bname)
  },
  bindchange4: function(e) {
    var that = this;
    var index = e.detail.value
    console.log('picker发送选择改变，携带值为', (parseInt(index) + 1))
    this.setData({
      index4: index
    })
    console.log(that.data.fmaterial[that.data.index4])
  },
  bindchange5: function(e) {
    var that = this;
    var index = e.detail.value
    console.log('picker发送选择改变，携带值为', (parseInt(index) + 1))
    this.setData({
      index5: index
    })
    console.log(that.data.fsize[that.data.index5])
  },
  bindDateChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    })
  },
  //提交表单
  formSubmit: function(e) {
    console.log(e)
    var that = this;
    if (e.detail.value.fprice <= 0 || e.detail.value.fprice==""){
      wx.showToast({
        title: '服务费不能为0',
        icon:'none'
      })
      return false;
    }
    wx.request({
      url: app.globalData.urlPath + 'ManagerMent/identify',
      data: {
        fbrand: that.data.fbrand[that.data.index3].bname,
        fmaterial: that.data.fmaterial[that.data.index4],
        fsize: that.data.fsize[that.data.index5],
        fprice: e.detail.value.fprice,
        fmpic: app.globalData.pic,
        fsolve: e.detail.value.textarea1,
        fanalysis: e.detail.value.textarea,
        sessionid: that.data.sessionId,
        fendtime: that.data.date,
        id: e.detail.target.id
      },
      method: "POST",
      success: function(res) {
        console.log(res)
        if (res.data.code == 200) {
          wx.showToast({
            title: res.data.data,
            icon: 'success',
            duration: 2000,
            mask: true
          });
          app.globalData.pic = [];
          setTimeout(function() {
            wx.redirectTo({
              url: '/pages/masterOrderList/masterOrderList'
            })
          }, 2000)
        }
        if (res.data.code == 400) {
          wx.showToast({
            title: res.data.error,
            icon: 'none',
            duration: 2000,
            mask: true
          })
        }
      }
    })
  },
  //底部导航切换
  handleChangeTab({
    detail
  }) {
    var that = this;
    console.log(detail.key)
    if (detail.key == 'order') {
      wx.redirectTo({
        url: '/pages/masterOrderList/masterOrderList',
      })
    } else if (detail.key == 'code') {
      // 只允许从相机扫码
      wx.scanCode({
        onlyFromCamera: true,
        success(res) {
          console.log(res);
          that.setData({
            qrcode: res.result
          })
          //加载二维码返回数据
          wx.request({
            url: app.globalData.urlPath + '/ManagerMent/scanqrcode',
            data: {
              sessionid: app.globalData.sessionid,
              qrcode: that.data.qrcode
            },
            method: "POST",
            success: function(res) {
              console.log(res)
              if (res.data.code == 200) {
                that.setData({
                  orderInfo: res.data.data,
                  // fbrand: res.data.data.fbrand,
                  // fmaterial: res.data.data.fmaterial,
                  // fsize: res.data.data.fsize,
                })
                wx.redirectTo({
                  url: '../masterSubOrder/masterSubOrder?qrcode=' + that.data.qrcode,
                })
              } else {
                wx.showToast({
                  title: res.data.error,
                  icon: 'none',
                  duration: 2000
                })
                if (res.data.code == 5000) {
                  wx,
                  wx.redirectTo({
                    url: '/pages/login/login',
                  })
                }
              }
            }
          })

        }
      })

    } else if (detail.key == 'my') {
      wx.redirectTo({
        url: '/pages/masterMy/masterMy',
      })
    }
    this.setData({
      currentTab: detail.key
    });
  },
  totime:function(t){
    let T=new Date(t*1000)
    let text=T.getFullYear()+"/"+(T.getMonth()+1)+"/"+T.getDate()+"  "+T.getHours()+":"+T.getMinutes();
    return text;
  }
})